package com.gaimall.beans.pagebean;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.gaimall.beans.pagebean.element.Detail_item;
import com.gaimall.beans.pagebean.element.Detail_item_sub;
import com.gaimall.beans.xmlbean.Result_idsearch;

/**
 * detail 页面的bean类
 * 
 * @author Ocelot
 * 
 */
public class DetailPage {

	private List<Detail_item> l_di = new ArrayList<Detail_item>();

	private String main_title;
	private String detail_left_pic;
	private String brand_title;
	private String type_title;
	private String price_title;

	private String price_history_all;

	private final static Map<String, String> shopmap = new HashMap<String, String>();

	public DetailPage(Result_idsearch rs, String kw, String page) {
		this();

		if (rs.getLength().equals("0")) {
			return;
		}
		String[] source_all = rs.getGoods_list().get(0).getSource().split("어", -1);
		String[] urls = rs.getGoods_list().get(0).getUrl().split("어", -1);
		String[] pic_urls = rs.getGoods_list().get(0).getPic_url().split("어", -1);
		String[] prices = rs.getGoods_list().get(0).getPrice().split("어", -1);
		String[] price_historys = rs.getGoods_list().get(0).getPrice_history().split("어", -1);
		String[] names = rs.getGoods_list().get(0).getName().split("어", -1);

		Set<String> source_first = new HashSet<String>();
		for (String e : source_all) {
			String[] source = e.split("_", -1);
			source_first.add(source[0]);
		}

		for (String e : source_first) {
			Detail_item di = new Detail_item();
			di.setSource(e);
			l_di.add(di);
		}
		for (Detail_item e : l_di) {
			for (int i = 0; i < source_all.length; i++) {
				if (source_all[i].split("_", -1)[0].equals(e.getSource())) {
					Detail_item_sub dis = new Detail_item_sub();
					dis.setUrl(urls[i]);
					dis.setPrice(prices[i]);
					dis.setPic_url(pic_urls[i]);

					List<String> phl = new ArrayList<String>();
					String[] prices_ph = price_historys[i].split("\\|", -1);
					for (String prices_ph_e : prices_ph) {
						phl.add(prices_ph_e);
					}
					dis.setPrice_history(phl);
					
					//计算价格趋势
					try {
						if(prices_ph.length > 1){
							String price1 = prices_ph[prices_ph.length-2].split("=",-1)[1].replace(",", "").replace("￥", "").replace("$", "");
							String price2 = prices_ph[prices_ph.length-1].split("=",-1)[1].replace(",", "").replace("￥", "").replace("$", "");
							
							if(Double.parseDouble(price2)>Double.parseDouble(price1)){
								dis.setPrice_trend("up");
							}else{
								dis.setPrice_trend("down");
							}
						}else{
							dis.setPrice_trend("down");
						}
					} catch (NumberFormatException e1) {
						dis.setPrice_trend("down");
					}
					
					String[] source_subs = source_all[i].split("_", -1);
					if (source_subs.length > 1) {
						dis.setSub_source(source_subs[1]);
					} else {
						dis.setSub_source(shopmap.get(source_subs[0]));
					}

					dis.setName(names[i]);
					e.getDis().add(dis);
				}

			}

			Collections.sort(e.getDis());
		}

		// fill attribute
		this.detail_left_pic = pic_urls[0];
		this.brand_title = rs.getGoods_list().get(0).getBrand();
		this.type_title = rs.getGoods_list().get(0).getSecond_type();
		this.main_title = rs.getGoods_list().get(0).getStandard_name();

		try {
			double max_price = Double.parseDouble(prices[0].replace(",", ""));
			double min_price = Double.parseDouble(prices[0].replace(",", ""));

			for (String e : prices) {
				double e_price = Double.parseDouble(e.replace(",", ""));
				max_price = max_price < e_price ? e_price : max_price;
				min_price = min_price > e_price ? e_price : min_price;
			}

			if (min_price != max_price) {
				this.price_title = min_price + "~" + max_price + "元";
			} else {
				this.price_title = max_price + "元";
			}

		} catch (NumberFormatException nfe) {
			this.price_title = "价格未知";
		}

		// 价格趋势 all
		this.price_history_all = rs.getGoods_list().get(0).getPrice_history().replace(",", "");

	}

	public DetailPage() {
		shopmap.put("amazon", "亚马逊");
		shopmap.put("binggo", "缤购");
		shopmap.put("caomeipai", "草莓派");
		shopmap.put("chinadrtv", "橡果国际");
		shopmap.put("cm365", "成名网");
		shopmap.put("coo8", "库巴购物网");
		shopmap.put("cosmede", "玫丽网");
		shopmap.put("dangdang", "当当网");
		shopmap.put("dao", "道网");
		shopmap.put("dwgou", "道位购");
		shopmap.put("easy361", "易斯来福商城");
		shopmap.put("efeihu", "飞虎乐购");
		shopmap.put("gome", "国美电器");
		shopmap.put("goodjd", "品牌家电网");
		shopmap.put("heroone", "华英雄");
		shopmap.put("jumei", "聚美");
		shopmap.put("lafaso", "乐蜂");
		shopmap.put("leftlady", "左岸女人");
		shopmap.put("likeface", "likeface");
		shopmap.put("liqunshop", "利群商城");
		shopmap.put("lusen", "绿森数码");
		shopmap.put("m18", "麦考林");
		shopmap.put("mobil89", "移宝网");
		shopmap.put("moonbasa", "梦芭莎");
		shopmap.put("new7", "新七天电器");
		shopmap.put("newegg", "新蛋购物");
		shopmap.put("no5", "No5");
		shopmap.put("ocj", "东方cj");
		shopmap.put("orange3c", "品橙网");
		shopmap.put("ouku", "欧酷");
		shopmap.put("shjdq", "盛家电器网");
		shopmap.put("shnn", "申网商城");
		shopmap.put("shougoo", "首购网");
		shopmap.put("suning", "苏宁易购");
		shopmap.put("szjdw", "深圳家电网");
		shopmap.put("taobao", "天猫");
		shopmap.put("vancl", "凡客");
		shopmap.put("vjia", "vjia");
		shopmap.put("w139shop", "北斗星手机网");
		shopmap.put("w360buy", "京东商城");
		shopmap.put("w51buy", "易讯网");
		shopmap.put("w51ppll", "漂亮无忧");
		shopmap.put("w529buy", "鸿信通");
		shopmap.put("xiu", "走秀网");
		shopmap.put("yidianda", "一点达");
		shopmap.put("yihaodian", "1号店");
		shopmap.put("yingtao", "樱桃网");
		shopmap.put("youpin", "悠品");
		shopmap.put("zhongyu", "中域商城");
		shopmap.put("redbaby", "红孩子");
		shopmap.put("muyingzhijia", "母婴之家");
		shopmap.put("m6go", "麦乐购");
		shopmap.put("lvhezi", "绿盒子");
		shopmap.put("aiyingshi", "爱婴室");
		shopmap.put("leyou", "乐友");
		shopmap.put("qinqinbaby", "亲亲宝贝");
		shopmap.put("boshiwa", "博士蛙");
		shopmap.put("lijiababy", "丽家宝贝");
		shopmap.put("minitiao", "迷你挑");
		shopmap.put("tbb", "特宝贝");
		shopmap.put("wbiao", "万表网");
		shopmap.put("ppbiao", "翡表网");
		shopmap.put("ctf", "周大福");
		shopmap.put("yintai", "银泰网");
		shopmap.put("yoho", "有货");
		shopmap.put("mfplaza", "马克华菲");
		shopmap.put("luckcart", "热客");
		shopmap.put("w360mart", "买特网");
		shopmap.put("uiyi", "佑一良品");
		shopmap.put("letao", "乐淘");
		shopmap.put("htjz", "胡桃夹子");
		shopmap.put("lamiu", "兰缪");
		shopmap.put("menglu", "梦露时尚");
		shopmap.put("aimer", "爱慕");
		shopmap.put("vingz", "薇哲");
		shopmap.put("mbaobao", "麦包包");
		shopmap.put("xzuan", "新钻网");
		shopmap.put("jufengshang", "聚风尚");
		shopmap.put("w5lux", "第五大道");
		shopmap.put("d1", "d1优尚");
		shopmap.put("sealbeauty", "随尔美");
		shopmap.put("s", "名鞋库");
		shopmap.put("dazhe", "名品打折网");
		shopmap.put("yougou", "优购网");
		shopmap.put("taoxie", "淘鞋网");
		shopmap.put("paixie", "拍鞋网");
		shopmap.put("vcotton", "维棉");
		shopmap.put("kela", "珂兰钻石");
		shopmap.put("dionly", "戴欧妮");
		shopmap.put("okbuy", "好乐买");
		shopmap.put("w520", "戴维尼珠宝网");
		shopmap.put("onlyts", "OnlyTS");
		shopmap.put("daphne", "达芙妮");
		shopmap.put("aidai", "爱戴网");
		shopmap.put("yjcs", "亿超眼镜");
		shopmap.put("meijing", "美睛网");
		shopmap.put("sigo", "视客眼镜网");
		shopmap.put("easeeyes", "易视网");
		shopmap.put("myeyes", "爱视商城");
		shopmap.put("jiuxian", "酒仙网");
		shopmap.put("wine9", "品尚红酒");
		shopmap.put("yesmywine", "也买酒");
		shopmap.put("madeinworld", "美德因我");
		shopmap.put("winekee", "红酒客");
		shopmap.put("womai", "我买网");
		shopmap.put("winenice", "酒美网");
		shopmap.put("lepinjiu", "乐品酒");
		shopmap.put("w12580wine", "12580红酒");
		shopmap.put("sdwh", "时代万货");
	}

	public List<Detail_item> getL_di() {
		return l_di;
	}

	public void setL_di(List<Detail_item> l_di) {
		this.l_di = l_di;
	}

	public String getDetail_left_pic() {
		return detail_left_pic;
	}

	public void setDetail_left_pic(String detail_left_pic) {
		this.detail_left_pic = detail_left_pic;
	}

	public String getBrand_title() {
		return brand_title;
	}

	public void setBrand_title(String brand_title) {
		this.brand_title = brand_title;
	}

	public String getType_title() {
		return type_title;
	}

	public void setType_title(String type_title) {
		this.type_title = type_title;
	}

	public String getMain_title() {
		return main_title;
	}

	public void setMain_title(String main_title) {
		this.main_title = main_title;
	}

	public String getPrice_title() {
		return price_title;
	}

	public void setPrice_title(String price_title) {
		this.price_title = price_title;
	}

	public String getPrice_history_all() {
		return price_history_all;
	}

	public void setPrice_history_all(String price_history_all) {
		this.price_history_all = price_history_all;
	}

}
